// pages/Census/data-detail/index.js
const Controllers = require('../controllers/ControllerCensus.js').controller;
const agentStaffIncomeDetails = require('../controllers/ControllerMy.js').controller.agentStaffIncomeDetails;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabbar: app.globalData.tabbar,
    header: {
      title: '资金统计',
      detail: '查看明细',
      url: '/pages/Census/fund-detail/index',
      status: 1,
      childs: [
        {
          laber: '总收益',
          value: '',
          key: 'totalProfit',
          unit: '¥',
          url: '',
          className: ''
        },
        {
          laber: '租金收益',
          value: '1000.25',
          key: 'rent',
          unit: '¥',
          url: '',
          className: 'fontColor'
        },
        {
          laber: '云商品收益',
          value: '',
          key: 'cloud',
          unit: '¥',
          url: '',
          className: 'fontColor'
        },
        {
          laber: '客户数量',
          value: '',
          key: 'shopNum',
          unit: '',
          url: '',
          className: 'fontColor'
        }
      ]
    },
    tabs: [
      {
        text: '租金收益',
        status: 8
      },
      {
        text: '云商品收益',
        status: 7
      },
      {
        text: '客户数量',
        status: 0
      }
    ],
    active: 0,
    page: {
      pageNumber: 1,
      pageSize: 10,
    },
    list: [],
    totalRow: 0,
    isLoading: false,
    lastPage: true,
    commission: '',
    withdrawal: '',
    balance: '',
    agentStaffIncomeDetails: {}
  },
  moneyStatisticsPage() {
    var that = this;
    var params = {
      pageNumber: that.data.pageNumber,
      pageSize: that.data.pageSize,
      type: that.data.tabs[that.data.active].status,
      commission: 1
      // commission: that.data.pageNumber === 1 ? 1 : 0
    };
    wx.showLoading();
    Controllers.moneyStatisticsPage(params).then(res => {
      console.log('moneyStatisticsPage--res==', res);
      const { code, data, desc } = res;
      const { rent, cloud, agentStaffCommission, page } = data;
      that.data.header.childs[0].value = parseFloat(rent + cloud + agentStaffCommission).toFixed(2);
      that.setData({
        header: that.data.header,
        fundDetail: data,
        page,
        list: page.list || []
      });
      wx.hideLoading();
    });
  },

  agentStaffIncomeDetails() {
    
    var that = this;
    console.log('agentStaffIncomeDetails--that.data.isLoading==', that.data.isLoading);
    var page = that.data.page;
    if (that.data.isLoading) {
      return;
    }
    that.data.isLoading = true;
    wx.showLoading();
    const type = that.data.tabs[that.data.active].status;
    var params = { ...page };
    if (type) {
      params.type = type;
    }
    agentStaffIncomeDetails(params).then(res => {
      console.log('agentStaffIncomeDetails--res==', res);
      const { code, data, desc } = res;
      const { page, commission, withdrawal, totalRow, lastPage } = data;
      const list = page.list;
      if (page.pageNumber === 1) {
        that.data.list = list;
      } else {
        that.data.list = that.data.list.concat(list);
      }
      data.totalProfit = parseFloat(data.rent + data.cloud).toFixed(2);
      that.setData({
        details: data,
        commission,
        withdrawal,
        balance: parseFloat(commission - withdrawal).toFixed(2),
        list: list || [],
        totalRow,
        lastPage,
        isLoading: false
      });
      wx.hideLoading();
    }).catch(err => {
      wx.showToast({
        title: err,
        icon: 'none'
      })
      that.setData({
        isLoading: false
      })
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.setData({
      tabbar: app.globalData.tabbar
    })
    that.agentStaffIncomeDetails();
  },
  changeTab(e) {
    var that = this;
    var index = parseInt(e.currentTarget.dataset.index);
    if (index === that.data.active) {
      return;
    }
    that.setData({
      page: {
        pageNumber: 1,
        pageSize: 10
      },
      active: index
    })
    that.agentStaffIncomeDetails();
  },
  tabChange(e) {
    app.tabChange(e);
  },
  openPage(e) {
    var url = e.currentTarget.dataset.url;
    if (url) {
      app.openPage({ url });
    } else {
      wx.showToast({
        title: '未获取页面路径',
        icon: 'none'
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})